/**
 * Copyright (c) 2013 onionframework.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.onionframework.code.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.Range;
import org.onionframework.persistence.model.AbstractModel;
import org.onionframework.persistence.model.annotation.DomainModel;

/**
 * 业务代码
 * @author allan
 *
 */
@Entity
@Table(name="T_CODE_CODE")
@DomainModel(name="业务代码")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Code extends AbstractModel{

	private static final long serialVersionUID = -122424574808662087L;
	
	private String name;			//代码名称，一般来说是汉字
	private String value;			//代码值，一般来说是某种编码
	private boolean enabled = true;
	private CodeType type;
	private Integer index = 0;

	/**
	 * @return the name
	 */
	@NotBlank(message="名称不能为空")
	@Length(min = 1, max = 100, message="业务代码名称长度范围{min}-{max}")
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the value
	 */
	@NotBlank(message="值不能为空")
	@Length(min = 1, max = 100, message="业务代码值长度范围{min}-{max}")
	public String getValue() {
		return value;
	}

	/**
	 * @param value the value to set
	 */
	public void setValue(String value) {
		this.value = value;
	}

	/**
	 * @return the enabled
	 */
	public boolean isEnabled() {
		return enabled;
	}

	/**
	 * @param enabled the enabled to set
	 */
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	/**
	 * @return the type
	 */
	@NotNull(message="类型不能为空")
	@ManyToOne(fetch=FetchType.LAZY)
	public CodeType getType() {
		return type;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(CodeType type) {
		this.type = type;
	}
	
	public String toString(){
		return this.name;
	}

	/**
	 * @return the order
	 */
	@Column(name="order_index")
	@Range(min=Long.MIN_VALUE,message="顺序号应该是数字")
	public Integer getIndex() {
		return index;
	}

	/**
	 * @param order the order to set
	 */
	public void setIndex(Integer index) {
		this.index = index;
	}
}
